﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Threading.Tasks;
using System.Web;


namespace AsyncDemo
{
    class Program
    {
        static void Main(string[] args)
        {

            using (var db = new BlogContext())
            {
                var blogs = db.Blogs.ToList();
                foreach (var blog in blogs)
                {
                    Console.WriteLine(blog.BlogId + "\t" + blog.ListDisplayName);
                }
                
            }

            Console.ReadKey();

            #region

            //using (var db = new BlogContext())
            //{
            //    // Create and save a new Blog
            //    Console.Write("Enter a name for a new Blog: ");
            //    var name = Console.ReadLine();

            //    var blog = new Model.Blog { Title = name, Url = "http://www.baidu.com" };
            //    db.Blogs.Add(blog);
            //    db.SaveChanges();

            //    // Display all Blogs from the database
            //    var query = from b in db.Blogs
            //                orderby b.Title
            //        select b;

            //    Console.WriteLine("All blogs in the database:");
            //    foreach (var item in query)
            //    {
            //        Console.WriteLine(item.Title);
            //    }

            //    Console.WriteLine("Press any key to exit...");
            //    Console.ReadKey();
            //}

            #endregion

            #region

            /*using (var db = new BloggingContext())
            {
                // Create and save a new Blog
                Console.Write("Enter a name for a new Blog: ");
                var name = Console.ReadLine();

                var blog = new Blog { Name = name, };
                db.Blogs.Add(blog);
                db.SaveChanges();

                // Display all Blogs from the database
                var query = from b in db.Blogs
                    orderby b.Name
                    select b;

                Console.WriteLine("All blogs in the database:");
                foreach (var item in query)
                {
                    Console.WriteLine(item.Name);
                }

                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
            }*/

            #endregion

            #region 001

            /* 001
            PerformDatabaseOperations();

            Console.WriteLine("Quote of the day");
            Console.WriteLine(" Don't worry about the world coming to an end today... ");
            Console.WriteLine(" It's already tomorrow in Australia.");

            Console.WriteLine();
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
            */

            #endregion
        }

        #region 异步操作 VS2013 EF6.0 net4.0 好像不支持
        //异步操作 VS2013 EF6.0 net4.0 好像不支持
        //public static async Task PerformDatabaseOperationsAsync()
        //{

        //    using (var db = new BloggingContext())
        //    {
        //        db.Blogs.Add(new Blog
        //        {
        //            Name = "Test Blog #" + (db.Blogs.Count() + 1)
        //        });
        //        Console.WriteLine("Calling SaveChanges.");
        //        await db.SaveChangesAsync();
        //        Console.
        //    }
        //}
        #endregion


        #region 001
        //同步操作 001
        //public static void PerformDatabaseOperations()
        //{
        //    using (var db = new BloggingContext())
        //    {
        //        // Create a new blog and save it
        //        db.Blogs.Add(new Blog
        //        {
        //            Name = "Test Blog #" + (db.Blogs.Count() + 1)
        //        });
        //        Console.WriteLine("Calling SaveChanges.");
        //        db.SaveChanges();
        //        Console.WriteLine("SaveChanges completed.");

        //        // Query for all blogs ordered by name
        //        Console.WriteLine("Executing query.");
        //        var blogs = (from b in db.Blogs
        //            orderby b.Name
        //            select b).ToList();

        //        // Write all blogs out to Console
        //        Console.WriteLine("Query completed with following results:");
        //        foreach (var blog in blogs)
        //        {
        //            Console.WriteLine(" " + blog.Name);
        //        }
        //    }
        //} //end PerformDatabaseOperations
        #endregion 001
    }


    //public class BloggingContext : DbContext
    //{
    //    public BloggingContext()
    //        : base("name=BloggingContext")
    //    {

    //    }
    //    public DbSet<Blog> Blogs { get; set; }
    //    public DbSet<Post> Posts { get; set; }
    //}

    //public class Blog
    //{
    //    public int BlogId { get; set; }
    //    public string Name { get; set; }

    //    public virtual List<Post> Posts { get; set; }
    //}

    //public class Post
    //{
    //    public int PostId { get; set; }
    //    public string Title { get; set; }
    //    public string Content { get; set; }

    //    public int BlogId { get; set; }
    //    public virtual Blog Blog { get; set; }
    //}
}
